package com.rabbit.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.DeviceType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.LoginService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.UserLoginReq;
import com.rabbit.vo.res.LoginRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <h1>登录登出</h1>
 *
 * @author zjw
 * @date 2022/1/12 8:42
 */
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 系统登录
     */
    @PostMapping("/login")
    public ResultVO<Object> login(@Validated @RequestBody UserLoginReq userLoginReq, HttpServletRequest request) throws BusinessException {
        LoginRes loginRes = loginService.login(userLoginReq, request);
        return ResultGenUtil.genSuccess(loginRes);
    }

    /**
     * 系统登出
     */
    @PostMapping("/logout")
    public ResultVO<Object> loginOut() {
        StpUtil.logoutByTokenValue(StpUtil.getTokenValue());
        return ResultGenUtil.genSuccess();
    }

}
